/**
 * Created by admin on 2017/4/11.
 */
define(['app', 'filters/filters'], function(app) {
    app.register.controller('signUp', function ($scope, $http, $state, $sce,$stateParams,$rootScope,$timeout,$modal,$cookieStore,$prompt) {
        $cookieStore.put("tiaozhuan_sign",0);
        // $rootScope.hasHeader = 1;//没有顶部的header
        $scope.page=1;
        $scope.values = '';
        $scope.thisShow = {
            message_show:false,
            key:false,
            phone_mes:false,
            yzm_err:false,
            name_shop_err:false,
            request_most:false,
            phone_err:false,
            ww_err:false,
            email_err:false,
            email_mes:false,
            gongsi_err:false,
            dianpu_err:false,
            lianxiren_err:false,
            quyu_err:false,
            hide:function(){
                this.message_show = false;
                this.key = false;
            },
            show:function(){
                this.message_show = true;
                this.key = true;
            }

        }
        $scope.signUpInfo={
            customer:'',
            phone:'',
            email:'',
            province:'',
            city:'',
            name:'',
            ww:'',
            card_id:'',
            product_id:''
        };
        //获取验证码
        $rootScope.request_num = 0;
        $scope.getCode = function(){
            if($rootScope.request_num > 5){
                $scope.thisShow.request_most = true;
                return false;
            }
            if(!$scope.nameOfshop){
                $scope.thisShow.name_shop_err = true;
                return false;
            }
            $http({
                url: app._baseUrl + '/Qy/Topic/sendCode',
                method: 'POST',
                data:{
                    customer:$scope.nameOfshop
                },
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).success(function(res){
                if(res.success){
                    $rootScope.request_num++;
                    $scope.message = res.message;
                    $scope.thisShow.show();//验证码已发送。。。显示
                    $scope.sec = 59;//59秒倒计时
                    var interval = setInterval(function() {//设置定时器
                        if($scope.sec > 0) {
                            $scope.sec--;
                        } else {
                            clearInterval(interval);
                            $scope.thisShow.key = false;
                        }
                        $scope.$digest();//脏数据检验
                    }, 1000);

                }else{//

                }
            })
        }
        // 第一页下一步
        $scope.next = function(){
            //判断是否填写两项
            $scope.blurAll.blur_yzm();$scope.blurAll.blur_name_shop();
            if($scope.blurAll.blur_yzm()&&$scope.blurAll.blur_name_shop()){
                $http({
                    url: app._baseUrl + 'Qy/Topic/checkCustomer',
                    method: 'POST',
                    data:{
                        customer:$scope.nameOfshop,//记得问后台这个号码在哪里返回的
                        verifyCode:$scope.yzm
                    },
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                }).success(function(res){
                    if(res.success){
                        $scope.yanzhengma = $scope.yzm;
                        $scope.dianpu = $scope.nameOfshop;
                        $scope.gongsi_name = '';$scope.dianpu_name = '';$scope.lianxiren_name = '';$scope.phone = '';$scope.ww = '';$scope.email_yx = '';$scope.values = '';$scope.citys = '';
                        $scope.thisShow = {
                            message_show:false,
                            key:false,
                            phone_mes:false,
                            name_shop_err:false,
                            yzm_err:false,
                            request_most:false,
                            phone_err:false,
                            ww_err:false,
                            email_err:false,
                            email_mes:false,
                            gongsi_err:false,
                            dianpu_err:false,
                            lianxiren_err:false,
                            quyu_err:false,
                            hide:function(){
                                this.message_show = false;
                                this.key = false;
                            },
                            show:function(){
                                this.message_show = true;
                                this.key = true;
                            }
                        }
                        $scope.page=2;
                    }else{//验证失败
                        if(res.code==-103){
                            $prompt.timeout($scope, {code: '', message: '您尚未登录，无法进行任何操作，请登录后重试！'});
                            $cookieStore.remove('userInfo');
                            $timeout(function () {
                                $state.go('access.login');
                            }, 300);
                        }else{
                            $scope.thisShow.message_show = true;
                            $scope.message = '验证码输入错误';
                            $scope.thisShow.key = false;
                        }
                    }
                })
            }else{

            }

        }
        // 第2页上一步
        $scope.prev = function(){
            $scope.nameOfshop = '';
            $scope.yzm = ''
            $scope.thisShow.key = false;
            $scope.page=1;
        }
        //失焦事件
        $scope.blurAll = {
            blur_name_shop:function(){
                if(!$scope.nameOfshop){
                    $scope.thisShow.name_shop_err = true;
                    return false;
                }else{
                    $scope.thisShow.name_shop_err = false;
                    return true;
                }
            },
            blur_yzm:function(){
                if(!$scope.yzm){
                    $scope.thisShow.yzm_err = true;
                    return false;
                }else{
                    $scope.thisShow.yzm_err = false;
                    return true;
                }
            }
            ,
            blur:function(){//失焦事件
                if(!$scope.phone) {//没有输入账号
                    $scope.thisShow.phone_mes = false;
                    $scope.thisShow.phone_err = true;
                    return false;
                }else{

                    return true;
                }
            },
            blur_ww : function(){
                if(!$scope.ww){
                    $scope.thisShow.ww_err = true;
                    return false;
                }else{
                    $scope.thisShow.ww_err = false;
                    return true;
                }

            },
            blur_yx : function(){
                if(!$scope.email_yx){
                    $scope.thisShow.email_mes = false;
                    $scope.thisShow.email_err = true;
                    return false;
                }else{

                    return true;
                }
            },
            blur_gongsi:function(){
                if(!$scope.gongsi_name){
                    $scope.thisShow.gongsi_err = true;
                    return false;
                }else{
                    $scope.thisShow.gongsi_err = false;
                    return true;
                }
            },
            // blur_dianpu : function(){
            //     if(!$scope.dianpu_name){
            //         $scope.thisShow.dianpu_err = true;
            //         return false;
            //     }else{
            //         $scope.thisShow.dianpu_err = false;
            //         return true;
            //     }
            // },
                blur_lianxiren :function(){
                if(!$scope.lianxiren_name){
                    $scope.thisShow.lianxiren_err = true;
                    return false;
                }else{
                    $scope.thisShow.lianxiren_err = false;
                    return true;
                }
            }
        }
        //省市显示
        $scope.getProvince=function(){
            $http({
                url: app._baseUrl + '/Qy/Topic/area&level=1&upid=0',
                method: 'GET',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            }).success(function (res) {
                $scope.provinceList = res.data;
            });
        }
        $scope.getProvince();
        $scope.getCity=function(){
            $http({
                url: app._baseUrl + '/Qy/Topic/area&level=2&upid='+$scope.signUpInfo.province,
                method: 'GET',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            }).success(function (res) {
                $scope.cityList = res.data;
            });
        }
        // 省联动刷新市
        $scope.$watch('signUpInfo.province', function(n, o) {
            if (n) {
                $scope.getCity();
            }
        });




        //鼠标弹起
        $scope.keyup = function(){
            if($scope.phone){//非空
                $scope.thisShow.phone_err = false;
                var len = $scope.phone.length;
                var exp=/^1[3|4|5|7|8][0-9]{9}$/;//验证账号正则
                //console.log($scope.phone)
                if(len<11||len == 11&&exp.test($scope.phone)==false){//账号为11位，但不是账号
                    $scope.thisShow.phone_mes = true;
                    return false;
                }
                if(len == 11&&exp.test($scope.phone)==true) {//账号输入正确
                    $scope.thisShow.phone_mes = false;
                    return true;
                }
            }else{
                return false;
            }
        }
        //邮箱
        $scope.keup_yx = function(){
            if($scope.email_yx){
                $scope.thisShow.email_err = false;
                var exp_yx =/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
                if(exp_yx.test($scope.email_yx)==false){
                    $scope.thisShow.email_mes = true;
                    return false;
                }else{
                    $scope.thisShow.email_mes = false;
                    return true;
                }
            }else{

                return false;
            }
        }
        //区域
        $scope.keyup_sheng = function(){
            if(!$scope.signUpInfo.province||!$scope.signUpInfo.city){
                $scope.thisShow.quyu_err = true;
                return false;
            }else{
                $scope.thisShow.quyu_err = false;
                return true;
            }
        }
        $scope.keyup_shi = function(){
            if(!$scope.signUpInfo.province||!$scope.signUpInfo.city){
                $scope.thisShow.quyu_err = true;
                return false;
            }else{
                $scope.thisShow.quyu_err = false;
                return true;
            }
        }
        //提交信息
        $scope.pay = function(){
            $scope.keyup_sheng();$scope.keyup_shi();$scope.keup_yx();$scope.keyup();$scope.blurAll.blur();$scope.blurAll.blur_ww();$scope.blurAll.blur_yx();$scope.blurAll.blur_gongsi();$scope.blurAll.blur_lianxiren();
            if($scope.keyup_sheng()&&$scope.keyup_shi()&&$scope.keup_yx()&&$scope.keyup()&&$scope.blurAll.blur()&&$scope.blurAll.blur_ww()&&$scope.blurAll.blur_yx()&&$scope.blurAll.blur_gongsi()&&$scope.blurAll.blur_lianxiren()){
                // $http({
                //     url: app._baseUrl + 'Qy/Topic/apply',
                //     method: 'POST',
                //     data:{
                //         company:$scope.gongsi_name,
                //         phone:$scope.phone,
                //         actor:$scope.lianxiren_name,
                //         ww:$scope.ww,
                //         province:$scope.signUpInfo.province,
                //         city:$scope.signUpInfo.city,
                //         customer:$scope.dianpu,
                //         verifyCode:$scope.yanzhengma,
                //         email:$scope.email_yx
                //     },
                //     headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                // }).success(function(res){
                //         if (res.success) {
                //             var code=res.data;
                //             $scope.code = res.data;
                //             window.open($scope.code,'_blank');
                //             $scope.page = 3;
                //         }
                // })
                var data = {
                    company:$scope.gongsi_name,
                    phone:$scope.phone,
                    actor:$scope.lianxiren_name,
                    ww:$scope.ww,
                    province:$scope.signUpInfo.province,
                    city:$scope.signUpInfo.city,
                    customer:$scope.dianpu,
                    verifyCode:$scope.yanzhengma,
                    email:$scope.email_yx
                }
                var data = JSON.stringify(data);
                var url = $state.href('app.pay',{
                    paydata:data
                })
                window.open(url,'_blank');
                $scope.page = 3;
                window.onbeforeunload=null;
            }
            else{
                //console.log(0)
            }
        }
        window.onbeforeunload = function(event){
            return "您确定要退出页面吗？";
        }
        $scope.iKnow = function(){
            // window.close();
            $state.go('app.proIndex');
        }
        $scope.again_pay = function(){
            var data = {
                company:$scope.gongsi_name,
                phone:$scope.phone,
                actor:$scope.lianxiren_name,
                ww:$scope.ww,
                province:$scope.signUpInfo.province,
                city:$scope.signUpInfo.city,
                customer:$scope.dianpu,
                verifyCode:$scope.yanzhengma,
                email:$scope.email_yx
            }
            var data = JSON.stringify(data);
            var url = $state.href('app.pay',{
                paydata:data
            })
            window.open(url,'_blank');
            $scope.page = 3;
            window.onbeforeunload=null;
        }
    })
})